/*
 * Lista.cpp
 *
 *  Created on: Oct 15, 2011
 *      Author: lore
 */

#include "Lista.h"

Lista::Lista() {
}

void Lista::setIdEleccion(unsigned long idelecc){
	idEleccion=idelecc;
}

unsigned long Lista::getIdEleccion(){
	return idEleccion;
}

void Lista::setId(unsigned long _id) {
	id = _id;
}
unsigned long Lista::getId() {
	return id;
}


void Lista::setNombreLista(string nomlista){
		nombreLista=nomlista;
}

string Lista::getNombreLista(){
	return nombreLista;
}

char* Lista::pack() {
	char* buffer = new char[packLen()];

	char*ptr = reinterpret_cast<char*>(&id);
		for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
			buffer[i] = *ptr;
			ptr++;
		}

	ptr = reinterpret_cast<char*>(&idEleccion);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i+sizeof(unsigned long)] = *ptr;
		ptr++;
	}
	
	unsigned long tam = nombreLista.size();
	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i+2*sizeof(unsigned long)] = *ptr;
		ptr++;
	}

	const char*str = nombreLista.c_str();
	for (unsigned long i = 0; i < nombreLista.size(); i++) {
		buffer[i+3*sizeof(unsigned long)] = str[i];
	}
	
	return buffer;
}

bool Lista::unpack(char* buffer, unsigned long bufferLen) {

	char*ptr = reinterpret_cast<char*>(&id);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i];
		ptr++;
	}

	ptr = reinterpret_cast<char*>(&idEleccion);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i+sizeof(unsigned long)];
		ptr++;
	}


	unsigned long tam = nombreLista.size();
	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i+2*sizeof(unsigned long)];
		ptr++;
	}
	stringstream ss;
	for (unsigned long i = 0; i < tam; i++) {
		ss << buffer[i+3*sizeof(unsigned long)];
	}
	nombreLista = ss.str();
	return true;
}

unsigned long Lista::packLen() {
	unsigned long tam= 3*sizeof(unsigned long) + nombreLista.size();
	return (tam);
}

Lista::~Lista() {
}

